//
//  WebViewController.m
//  OGL
//
//  Created by ZengYuan on 14/12/26.
//  Copyright (c) 2014年 ZengYuan. All rights reserved.
//

#import "WebViewController.h"
#import "UserUnit.h"
@interface WebViewController ()<UIGestureRecognizerDelegate,UIScrollViewDelegate>
@property (nonatomic,weak) UIButton *closeBtn;
@end

@implementation WebViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.view.backgroundColor = [UIColor whiteColor];
    
//    CGRect ii = self.view.bounds;
    if(!self.webView){
        self.webView=[[UIWebView alloc]initWithFrame:self.view.bounds];
        [self.view addSubview:self.webView];
    }
    self.webView.dataDetectorTypes=UIDataDetectorTypeAll^UIDataDetectorTypePhoneNumber;
    self.webView.delegate=self;
    self.webView.scalesPageToFit = YES;
    if(!CGRectIsEmpty(self.webFrame)){
        self.webView.frame=self.webFrame;
    }
    [super addFullloadView];
    [fullloadingView showLoadingState];
    
    Reachability*rea= [Reachability reachabilityForInternetConnection];
    if (![rea isReachable]){
         if (fullloadingView) {
             [fullloadingView showErrorMsg:HTTPERROR_CANNOTREACH];
         }else{
            MBALERT(HTTPERROR_CANNOTREACH);
        }
        return;
    }

    [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:self.url]]];
    // Do any additional setup after loading the view from its nib.
    self.webView.scrollView.scrollEnabled=NO;
    
    if (self.showRefreshView) {
        [self addHeaderPullFresh];
    }
    

}


//添加下拉刷新
-(void)addHeaderPullFresh{
    //    return;
    self.view.backgroundColor=[UIColor whiteColor];
    
    UIScrollView *scrlview = self.webView.scrollView;
    scrlview.backgroundColor=[UIColor whiteColor];
    scrlview.delegate=self;
    
    EGORefreshTableHeaderView *view = [[EGORefreshTableHeaderView alloc] initWithFrame:CGRectMake(0.0f, 0.0f - scrlview.bounds.size.height, scrlview.frame.size.width, scrlview.bounds.size.height)];
    view.backgroundColor=self.view.backgroundColor;
    view.delegate = self;
    [scrlview addSubview:view];
    _refreshHeaderView = view;

    [_refreshHeaderView refreshLastUpdatedDate];
    [self doneLoading];
    
}

//更新结束，下拉还原
- (void)doneLoading{
    _reloading = NO;
    if (_refreshHeaderView) {
        [_refreshHeaderView egoRefreshScrollViewDataSourceDidFinishedLoading:self.webView.scrollView];
    }
    
}



-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    
    [fullloadingView removeFromSuperview];
    fullloadingView = nil;
    
    if (self.closeBtn) {
        [self.closeBtn removeTarget:self action:@selector(navLeftAction) forControlEvents:UIControlEventTouchUpInside];
        [self.closeBtn removeFromSuperview];
        self.closeBtn = nil;
    }
}

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
    return YES;
}

- (void)webViewDidFinishLoad:(UIWebView *)_webView{
    [self doneLoading];
    //[hud hide:YES];
    [fullloadingView removeFromSuperview];
    fullloadingView = nil;
    self.webView.scrollView.scrollEnabled=YES;
}

- (void)webView:(UIWebView *)_webView
        didFailLoadWithError:(NSError *)error{
    
    [fullloadingView removeFromSuperview];
    fullloadingView = nil;
    
    self.webView.scrollView.scrollEnabled=YES;

}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    
    
//    if (self.navtitle.length > 10) {
//        NSString *tempStr = [[NSString alloc]initWithString:[self.navtitle substringToIndex:9]];
//        self.titleLabel.text=tempStr;
//
//    }else{
        self.titleLabel.text=self.navtitle;
//    }
    
}

- (void)setNavbarItem{
    
    [super setNavbarItem];
    
    UIButton *closeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [closeBtn addTarget:self action:@selector(navLeftAction) forControlEvents:UIControlEventTouchUpInside];
    self.closeBtn = closeBtn;
    [self.navigationController.navigationBar addSubview:closeBtn];
    
    [closeBtn setImage:[UIImage imageNamed:@"webclose"] forState:UIControlStateNormal];
    [closeBtn setImage:[UIImage imageNamed:@"webclose_h"] forState:UIControlStateHighlighted];

//    [closeBtn setTitle:@"关闭" forState:UIControlStateNormal];
    closeBtn.frame = CGRectMake(50, 2, 40, 40);
//    UIBarButtonItem *closeItem = [[UIBarButtonItem alloc] initWithCustomView:closeBtn];
    
    [self.backBtnPar removeTarget:self action:@selector(navLeftAction) forControlEvents:UIControlEventTouchUpInside];
    [self.backBtnPar  addTarget:self action:@selector(back:) forControlEvents:UIControlEventTouchUpInside];
//
//    
//    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithCustomView:self.backBtnPar];
//
//    self.navigationItem.leftBarButtonItems = [[NSArray alloc] initWithObjects:leftItem,closeItem, nil];
    
    
//    CGRect rec = self.titleLabel.frame;
//    rec.size.width = 80;
//    self.titleLabel.backgroundColor = [UIColor whiteColor];
//    self.titleLabel.frame=rec;
//    
//    self.navigationItem.titleView=nil;
//    self.titleLabel=nil;
//
//    return;
//    
//    self.titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 30)];
//    self.titleLabel.font = NAVBAR_FONT;
//    
//    self.titleLabel.textColor=[UIColor whiteColor];
//    self.titleLabel.backgroundColor=[UIColor clearColor];
//    
//    self.navigationItem.titleView=self.titleLabel;
//    self.titleLabel.textAlignment=NSTextAlignmentCenter;
}


- (void)back:(UIButton *)sender{
    if ([self.webView canGoBack]) {
           [self.webView goBack];
    }else{
        [self navLeftAction];
    }
 
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

#pragma mark -
#pragma mark UIScrollViewDelegate Methods

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    //    if (_refreshFooterView) {
    //        [_refreshFooterView egoRefreshScrollViewDidScroll:scrollView];
    //    }
    if (_refreshHeaderView) {
        [_refreshHeaderView egoRefreshScrollViewDidScroll:scrollView];
    }
    
    
    
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
    [_refreshHeaderView egoRefreshScrollViewDidEndDragging:scrollView];
    //    [_refreshFooterView egoRefreshScrollViewDidEndDragging:scrollView];
    
    
}


#pragma mark -
#pragma mark EGORefreshTableHeaderDelegate Methods

- (void)egoRefreshTableHeaderDidTriggerRefresh:(EGORefreshPos)aRefreshPos{
    
    [self reloadTableViewDataSource];
    
    if (aRefreshPos==EGORefreshHeader) {
        //重新刷新数据
        freshPos=EGORefreshHeader;
        //注意:下拉刷新的时候必须删除上拉刷新，ego的bug。原因暂未知。
        [self fresh];
    }
}

//检测当前是否正在更新网络数据
- (BOOL)egoRefreshTableHeaderDataSourceIsLoading:(UIView*)view{
    
    return _reloading; // should return if data source model is reloading
}

- (NSDate*)egoRefreshTableHeaderDataSourceLastUpdated:(UIView*)view{
    
    return [NSDate date]; // should return date data source was last changed
}

- (void)reloadTableViewDataSource{
    _reloading = YES;
}

-(void)fresh{
    [self.webView reload];
}

/*
 - (BOOL)shouldAutorotate{
    
    
    if (!OSVersionIsAtLeastiOS7) {
        return YES;
    }
    
    NSLog(@"========shouldAutorotate========");
    
    if (!self.viewStatus) {
        
        _viewStatus = [[UIView alloc]initWithFrame:CGRectMake(0, -20, self.view.frame.size.width, 64)];
        
    }
    
    if (![self.navigationController.navigationBar.subviews containsObject:_viewStatus]) {
        
        
        UIImageView * img = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 40)];
        
        
        
        img.backgroundColor = NAV_RED;
        
        img.alpha = 0.92;
        
        [_viewStatus addSubview:img];
        
        [self.navigationController.navigationBar addSubview:_viewStatus];
        
        [self.navigationController.navigationBar sendSubviewToBack:_viewStatus];
        
        _viewStatus.backgroundColor = [UIColor whiteColor];
        
    }
    
    CGRect frame = self.navigationController.navigationBar.frame;
    
    frame.origin.y = 20;
    
    frame.size.height = 64;
    
    self.navigationController.navigationBar.frame = frame;
    
    
    frame = self.view.frame;
    frame.origin.y=64;
    self.view.frame=frame;
 
    return [[UIDevice currentDevice] orientation] == UIInterfaceOrientationPortrait;
    
}
 */

@end
